function updateDatepickers(){
    $('.datepicker').each(function(i,v){
        var idAlt = "#alt" + v.id;
        $(v).removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/M/y', altField : idAlt, altFormat: 'dd/mm/y'});
    });
}


function validate() {
    $("#formEdit")
        .validate(
        {
            rules : {
                'centro.id': "required"
            }
        });

    $(".require").each(function (item) {
        $(this).rules("add", {
            required: true
        });
    });

    $(".data").each(function (item) {
        $(this).rules("add", {
            accept: "doc|docx|pdf",
            messages:{accept:"Only permitted doc, docx, or pdf."     }
        });
    });

}



function deleteEquipment(tableId, fila){
    var table = document.getElementById(tableId);
    var cant = document.getElementById("cantEquipment");

    var rows = table.getElementsByTagName('tr');
    var size = rows.length;
    for(var j = 0; j < rows.length; j++ ){
        if(rows[j].id == fila[0].id && rows[j].cells[1].id == fila[0].cells[1].id){
            break;
        }
    }

    if(cant.value == 1){
        var borrados = 0;
        var x = j - 1;
        while(borrados < 12){
            if(rows[x].cells[1] != null){
                rows[x].cells[1].children[0].value="";
                borrados++;
            }
            x--;
        }
        return;
    }

    var borrados = 0;
    var x = j;
    while(borrados < 13){
        rows[x].parentNode.removeChild(rows[x]);
        if(rows[x].cells[1] != null){
            borrados++;
        }
        x--;
    }

    var val = parseInt(cant.value,10) - parseInt(1,10);
    cant.value = val;

    var ultimo = document.getElementById("ultimoIdEquipment");
    var val2 = parseInt(cant.value,10) - parseInt(1,10);
    ultimo.value = val2;

    numerar();
}

function numerar(){
    $(".type").each(function(i){
        this.name = "equipmentcertificationList["+i+"].equipmentType.id";
        this.id= "typeCell"+i;
    });

    $(".serial").each(function(i){
        this.name = "equipmentcertificationList["+i+"].serialNumber";
        this.id= "serialNumberCell"+i;
    });

    $(".location").each(function(i){
        this.name = "equipmentcertificationList["+i+"].location";
        this.id= "locationCell"+i;
    });

    $(".calibration").each(function(i){
        this.name = "equipmentcertificationList["+i+"].calibrationFrequency";
        this.id= "calibrationCell"+i;
    });

    $(".last").each(function(i){
        this.name = "equipmentcertificationList["+i+"].lastcalibrationCell";
        this.id= "altlastcalibrationCell"+i;
    });

    $(".expiration").each(function(i){
        this.name = "equipmentcertificationList["+i+"].expirationDate";
        this.id= "altexpirationDateCell"+i;
    });

    $(".file").each(function(i){
        this.name = "equipmentcertificationList["+i+"].certificationFile";
        this.id= "docAttachmentCell"+i;
    });

    $(".certification").each(function(i){
        this.name = "equipmentcertificationList["+i+"].certification";
        this.id= "certificationCell"+i;
    });

    $(".id").each(function(i){
        this.name = "equipmentcertificationList["+i+"].id";
        this.id= "idCell"+i;
    });

    $(".del").each(function(i){
        this.id= "delEquipmentCell"+i;
    });



}

function addEquipment(tableID){
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    var cant = document.getElementById("cantEquipment");
    var val = parseInt(cant.value,10) + parseInt(1,10);
    cant.value = val;

    var ultimoId = document.getElementById("ultimoIdEquipment");

    for(var k = ultimoId.value; k>0; k--){
        if (document.getElementById("delEquipmentCell" + k) != null)
            break;
    }

    var newId = parseInt(ultimoId.value,10) + parseInt(1,10);
    ultimoId.value = newId;

    var idAfter = '#delEquipmentCell' + k;

    var row9 = $('#delEquipmentCell' + k).parent().clone()[0];
    row9.cells[1].id="delEquipmentCell"+newId;
    row9.cssClass="trNuevoDocumento";
    $(idAfter).parent().after(row9);

    var row81 = $('#idCell' + k).parent().parent().clone()[0];
    row81.cells[0].children[0].name="equipmentcertificationList["+newId+"].id";
    row81.cells[0].children[0].value="";
    row81.cells[0].children[0].id="idCell" + newId;
    $(idAfter).parent().after(row81);

    var row8 = $('#certificationCell' + k).parent().parent().clone()[0];
    row8.cells[0].children[0].name="equipmentcertificationList["+newId+"].certification";
    row8.cells[0].children[0].value="";
    row8.cells[0].children[0].id="certificationCell" + newId;
    $(idAfter).parent().after(row8);

    var row811 = $('#pepe').parent().clone()[0];
    $(idAfter).parent().after(row811);

    var row7 = $('#docAttachmentCell' + k).parent().parent().clone()[0];
    // row7.id="role" + newId + "degree";
    row7.cells[1].children[0].name="equipmentcertificationList[" + newId + "].certificationFile";
    row7.cells[1].children[0].value="";
    row7.cells[1].children[0].id="docAttachmentCell" + newId;
    $(idAfter).parent().after(row7);

    var row61 = $('#altexpirationDateCell' + k).parent().parent().clone()[0];
    row61.cells[0].children[0].name="equipmentcertificationList[" + newId + "].expirationDate";
    row61.cells[0].children[0].id="altexpirationDateCell" + newId;
    row61.cells[0].children[0].value="";
    $(idAfter).parent().after(row61);

    var row6 = $('#expirationDateCell' + k).parent().parent().clone()[0];
    //row6.cells[1].children[0].name="equipmentcertificationList[" + newId + "].expirationDate";
    row6.cells[1].children[0].value="";
    row6.cells[1].children[0].id="expirationDateCell" + newId;
    $(idAfter).parent().after(row6);

    initializeDate("#expirationDateCell" + newId ,"#altexpirationDateCell" + newId );

    var row51 = $('#altlastcalibrationCell' + k).parent().parent().clone()[0];
    row51.cells[0].children[0].name="equipmentcertificationList[" + newId + "].lastcalibrationCell";
    row51.cells[0].children[0].id="altlastcalibrationCell" + newId;
    row51.cells[0].children[0].value="";
    $(idAfter).parent().after(row51);

    var op = "#calibrationCell"+ newId;
    var op2 = "#altlastcalibrationCell"+ newId;
    var row5 = $('#lastcalibrationCell' + k).parent().parent().clone()[0];
    //row5.cells[1].children[0].name="equipmentcertificationList[" + newId + "].lastcalibrationCell";
    row5.cells[1].children[0].value="";
    row5.cells[1].children[0].onchange=function(){ calcularfecha( $(op).val(), $(op2).val(),  newId)  }
    row5.cells[1].children[0].id="lastcalibrationCell" + newId;
    $(idAfter).parent().after(row5);

    initializeDate("#lastcalibrationCell" + newId ,"#altlastcalibrationCell" + newId );

    var row4 = $('#calibrationCell' + k).parent().parent().clone()[0];
    row4.cells[1].children[0].name="equipmentcertificationList[" + newId + "].calibrationFrequency";
    row4.cells[1].children[0].value="";
    row4.cells[1].children[0].onchange=function(){calcularfecha( $(op).val(), $(op2).val(),  newId) };
    row4.cells[1].children[0].id="calibrationCell" + newId;
    $(idAfter).parent().after(row4);

    var row3 = $('#locationCell' + k).parent().parent().clone()[0];
    row3.cells[1].children[0].name="equipmentcertificationList[" + newId + "].location";
    row3.cells[1].children[0].value="";
    row3.cells[1].children[0].id="locationCell" + newId;
    $(idAfter).parent().after(row3);

    var row2 = $('#serialNumberCell' + k).parent().parent().clone()[0];
    row2.cells[1].children[0].name="equipmentcertificationList[" + newId + "].serialNumber";
    row2.cells[1].children[0].value="";
    row2.cells[1].children[0].id="serialNumberCell" + newId;
    $(idAfter).parent().after(row2);

    var row1 = $('#typeCell' + k).parent().parent().clone()[0];
    row1.cells[1].children[0].name="equipmentcertificationList[" + newId + "].equipmentType.id";
    row1.cells[1].children[0].value="";
    row1.cells[1].children[0].id="typeCell" + newId;
    $(idAfter).parent().after(row1);

}


function cerosIzq(sVal, nPos){
    var sRes = sVal;
    for (var i = sVal.length; i < nPos; i++)
        sRes = "0" + sRes;
    return sRes;
}

function armaFecha(nDia, nMes, nAno){
    var sRes = cerosIzq(String(nDia), 2);
    sRes = sRes + "/" + cerosIzq(String(nMes), 2);
    sRes = sRes + "/" + cerosIzq(String(nAno), 2);
    return sRes;
}

function sumaMes(nDia, nMes, nAno, nSum){
    if (nSum >= 0){
        for (var i = 0; i < Math.abs(nSum); i++){
            if (nMes == 12){
                nMes = 1;
                nAno += 1;
            } else nMes += 1;
        }
    } else {
        for (var i = 0; i < Math.abs(nSum); i++){
            if (nMes == 1){
                nMes = 12;
                nAno -= 1;
            } else nMes -= 1;
        }
    }
    return armaFecha(nDia, nMes, nAno);
}

function calcularfecha(meses, date, row){

    if(meses != '' && date != '' ){
        var sFc0 = date;
        var nSum = parseInt(meses);

        var sFc1 = sFc0;
        if (!isNaN(nSum)){
            var nDia = parseInt(sFc0.substr(0, 2));
            var nMes = parseInt(sFc0.substr(3, 2));
            var nAno = parseInt(sFc0.substr(6, 4));
            sFc1 = sumaMes(nDia, nMes, nAno+2000, nSum);
        }
        var exp = "#expirationDateCell" + row
       // $(exp).val(sFc1);
        //$(exp).datepicker( "option", "dateFormat", 'dd/M/y' );

        var split = sFc1.split("/");
        var str = split[1]+ "/" + split[0] + "/20" + split[2];
        var d = new Date(str);

        $(exp).datepicker( "option", "dateFormat", 'dd/mm/y' );
        $(exp).val( $.datepicker.formatDate('dd/mm/y', d));
        $(exp).datepicker( "option", "dateFormat", 'dd/M/y' );

    }


}